package com.cucn.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cucn.eduservice.entity.EduChapter;
import com.cucn.eduservice.entity.EduVideo;
import com.cucn.eduservice.entity.chapter.ChapterVO;
import com.cucn.eduservice.entity.chapter.VideoVO;
import com.cucn.eduservice.mapper.EduChapterMapper;
import com.cucn.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cucn.eduservice.service.EduVideoService;
import com.cucn.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author KouChaoJie
 * @since 2021-09-29
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    /**
     * 注入小节服务类,方便进行查询小节
     */
    @Autowired
    private EduVideoService videoService;

    /**
     * 根据课程id查询章节小节
     *
     * @param courseId 课程id
     * @return 对应课程的章节小节集合
     */
    @Override
    public List<ChapterVO> getChapterVideoByCourseId(String courseId) {
        //1.根据课程id查询课程里所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2.根据课程id查询课程所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //创建list集合用于封装最终数据
        List<ChapterVO> finalList = new ArrayList<>();

        //3.遍历查询章节list集合进行封装
        for (EduChapter eduChapter : eduChapterList) {
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(eduChapter, chapterVO);

            //创建集合,用于封装章节对应的小节
            List<VideoVO> videoVOList = new ArrayList<>();
            //4.遍历查询小节list集合进行封装
            for (EduVideo eduVideo : eduVideoList) {
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVO videoVO = new VideoVO();
                    BeanUtils.copyProperties(eduVideo, videoVO);
                    videoVOList.add(videoVO);
                }
            }

            chapterVO.setChildren(videoVOList);
            finalList.add(chapterVO);
        }

        return finalList;
    }

    /**
     * 删除章节
     *
     * @param chapterId 删除章节的对应id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        //先查询,如果章节下有小节,不让其删除
        //根据章节id查询小节表
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "章节下还有小节,不能删除!");
        } else {
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }

    /**
     * 根据课程id删除章节
     *
     * @param courseId 章节对应的课程id
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
